<?php
 
class mode_workClassAction extends inputAction{
	
	
	protected function savebefore($table, $arr, $id, $addbo){
		
	}
	
	
	protected function saveafter($table, $arr, $id, $addbo){

        header('Content-Type: application/json; charset=utf-8');

	    $distid = $arr['distid'];

        $wouser = m('wouser')->getrows("uid = '$distid'",'`uid`,`openid`');

        if(!count($wouser) || !strlen($distid)){

            return;
        }


        $openid = $wouser[0]['openid'];

        //请求url地址
        $appId = 'wxc2a0cb6fd674969a';
        $appSecret = 'af42fc23fa7be3f61270b7fee216cd45';
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
        //初始化curl
        $ch = curl_init($url);
        //3.设置参数
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        //4.调用接口
        $res = curl_exec($ch);
        if(curl_errno($ch)){
            var_dump(curl_error($ch));
        }
        $resArr = json_decode($res,1);
        //5.关闭curl
        curl_close($ch);

        $access_token = $resArr['access_token'];

        $Url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;   //模板信息请求地址

        $data = array(

            'first'=>array('value'=>"您好,有新的工单",'color'=>"#743A3A"),
            'keyword1'=>array('value'=>$arr['name'],'color'=>'#8C8C8C'),
            'keyword2'=>array('value'=>substr($arr['startdt'],0,16),'color'=>'#8C8C8C'),
            'keyword3'=>array('value'=>$arr['address'],'color'=>'#8C8C8C'),
            'keyword4'=>array('value'=>$arr['type'],'color'=>'#8C8C8C'),
            'keyword5'=>array('value'=>$arr['explain'],'color'=>'#8C8C8C'),
            'remark'=>array('value'=>'请按照上门时间尽快到达！','color'=>'#8C8C8C')
        );

        $template = array(
            'touser' => $openid,
            'template_id' => 'EWTLiCj9_4JHTt9pjxjalnOCrFYvTC2nmcWdISAweNo',
            'url' => 'http://oa.wxlion.com/task.php?a=x&num=work&mid='.$id.'&show=we',
            'topcolor' => 'red',
            'data' => $data
        );

        $post_data = json_encode($template);
        //发送数据，post方式<br>　　　　　　　　　//配置curl请求
        $ch = curl_init();//创建curl请求
        curl_setopt($ch, CURLOPT_URL,$Url); //设置发送数据的网址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值，0，直接显示
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
        curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
        //接收执行返回的数据
        $data = curl_exec($ch);
        //关闭句柄
        curl_close($ch);
        $data = json_decode($data,true); //将json数据转成数组

    }
	
	public function projectdata()
	{
		$rows 	= m('project')->getall('id>0 and status in(0,3)','`id`,`type`,`title`,`progress`','optdt desc');
		$arr	= array();
		foreach($rows as $k=>$rs){
			$arr[] = array(
				'name' => '['.$rs['type'].']'.$rs['title'].'('.$rs['progress'].'%)',
				'value' => $rs['id']
			);
		}
		return $arr;
	}

    public function customerdata()
    {
        $rows 	= m('customer')->getall('status = 1','`id`,`name`','optdt desc');
        $arr	= array();
        foreach($rows as $k=>$rs){
            $arr[] = array(
                'name' => $rs['name'],
                'value' => $rs['name']
            );
        }
        return $arr;
    }


}	
			